<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>充值中心</title>
    <link rel="stylesheet"  href="src/css/base.css?__version=9.4">
    <link rel="stylesheet"  href="src/css/pages.css?__version=9.4">
    <script type="text/javascript" src="src/js/jquery-1.10.1.min.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/rem.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/dropload.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/init.js?__version=9.4"></script>

	<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script src="/src/js/share.js?__version=9.4"></script>
	<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ed2efe5b24e14f351f4d88ba2eb3462";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</head>
<body>
	<div class="init_content" style="display: none;">
	    <div class="warp recharge-warp">
	        <section class="recharge-list" id='htmlDataList'></section>
	        <div class="recharge_way">
	        	<div class="recharge_box">
	        		<div class="title">
	        			支付方式
	        			<button class="cancel">取消</button>
	        		</div>
	        		<div class="recharge_money">
	        			<div class="item">充值金额 <span id="moneyed">10元</span></div>
	        			<div class="item"><a href="/myred.html">红包<span class="red" id="redbg">选择红包</span></a></div>
	        			<div class="item">实付款<span id="moneying">元</span></div>
	        		</div>
	        		<div class="recharge_waylist">
	        			<a href="javascript:void(0)" onclick="_hmt.push(['_trackEvent', '微信充值', 'click', '充值', 1])" data-type="1" style="display: none;" id="wechat"><img src="src/img/wxpay.png" />微信支付</a>
	        			<a href="javascript:void(0)" onclick="_hmt.push(['_trackEvent', '微信充值', 'click', '充值', 1])" data-type="3" style="display: none;" id="wechatwap"><img src="src/img/wxpay.png" />微信支付</a>
	        			 <a href="javascript:void(0)" onclick="_hmt.push(['_trackEvent', '支付宝支付', 'click', '充值', 1])" data-type="2" style="display: none" id="alipay"><img src="src/img/zfbpay.png" />支付宝支付</a>
	        		</div>
	        	</div>
	    </div>
	        </div>
	</div>
    <div class="init_loading">
        <img src="src/img/page_loading.gif?v=1">
    </div>
    <div class="rightfixe none">
        <div class="weixinguanzhubox">
            <button class="closebtn"><img src="src/img/login-close.png"></button>
            <div class="weixincontent">
                <div class="itemc" style="width:80%;margin:0 auto;">充值系统正在抢修，请扫描下方二维码关注公众号，发送“充值”。</div>
                <div class="img"><img src="/src/img/codeimg.jpg"></div>
                <div class="text">长按识别二维码</div>
            </div>
        </div>
    </div>
</body>
<script>
addheader("充值中心","javascript:history.back(-1);");
var red_id = getQueryString("red_id");
var myrednum=0;
var pay_rmb = 0;
$(function(){
		$(document).on("click",".recharge-item",function(){
			pay_rmb = $(this).attr("data-rmb");
			$(".recharge_way").show();
			$("#moneyed").text(pay_rmb+"元");
			getRedbackList(red_id,pay_rmb);
		})

	$(".recharge_way").click(function(e){
		var target=$(e.target);

    	if(!target.is(".recharge_box *")&&!target.is(".recharge_box")){
    		$(".recharge_way").hide();
    	}
	})
	$(document).on("click",".cancel",function(){
		$(".recharge_way").hide();
	})
	$(document).on("click",".weixinguanzhubox .closebtn",function(){
        $(".rightfixe").addClass("none");
    })
	$(document).on("click",".recharge_waylist a",function(){
		var pay_type = $(this).attr('data-type');
		console.log(red_id)
		var rmb = pay_rmb;
		if (pay_type == 1) {
			wechatpay(rmb,red_id);
		} else if (pay_type == 2) {
			aliPay(rmb,red_id);
		} else if (pay_type == 3) {
			// $(".rightfixe").removeClass("none");
			wechatwap(rmb,red_id);
		} else {
			alert("未知的充值方式");
		}
	})

	function wechatwap(rmb,red_back_id) {
		var action = "weChatWapPay";
		var token = cookieGet("token");
		var uid = cookieGet("uid");
		$.ajax({
			type: "post",
			url: api_url + "/OpenAPI/v1/Order/pay",
			jsonp: 'callback',
			dataType: 'jsonp',
			data: {
				action: action,
				uid: uid,
				rmb: rmb,
				token: token,
				red_back_id:red_back_id,
			},
			cache: false,
			timeout: 30000,
			success: function (result) {
				console.log(result);
				if (result.code == 0) {
					var data = result.data;
					if(data){
						window.location.href = data;
					}else{
						alert("充值成功")
						window.location.href = "/personal.html";
					}
					
				} else {
					alert(result.msg);
					$(".recharge_way").hide();
					window.location.reload();
				}
			},
			error: function () {
				alert('网络异常，请重试');
				window.location.reload();
			}
		});
	}

	function wechatpay(rmb,red_back_id) {
		var action = "wechatpay";
		var token = cookieGet("token");
		var openid = cookieGet("openid");
		var uid = cookieGet("uid");
		$.ajax( {
			type: "post",
			url:api_url+"/OpenAPI/v1/Order/pay",
			jsonp: 'callback',
			dataType:'jsonp',
			data:{
				action:action,
				uid: uid,
				rmb: rmb,
				token:token,
				openid:openid,
				red_back_id:red_back_id,
			},
			cache:false,
			timeout : 30000,
			success:function(result) {
				console.log(result);
				if (result.code == 0) {
					var data = result.data;
					if(data){
						WeixinJSBridge.invoke(
							'getBrandWCPayRequest', data,
							function(res){
								if(res.err_msg == "get_brand_wcpay_request:ok" ) {
									alert("充值成功");
									setTimeout(getCoin(),0.5);
								}
							}
						);
					}else{
						alert("充值成功");
						setTimeout(getCoin(),0.5);
					}
					
				}else{
					alert("登录状态异常!请重新登录.");
					logout();
					// alert(result.msg);
					// $(".recharge_way").hide();
					// window.location.reload();
				}
			},
			error : function() {
				alert('网络异常，请重试');
				window.location.reload();
			}
		});
	}

	/*支付宝支付*/
	function aliPay(rmb,red_back_id) {
		var action = "aliPayWap";
		var token = cookieGet("token");
		var uid = cookieGet("uid");
		$.ajax( {
			type: "post",
			url:api_url+"/OpenAPI/v1/Order/pay",
			jsonp: 'callback',
			dataType:'jsonp',
			data:{
				action:action,
				uid: uid,
				rmb: rmb,
				token:token,
				red_back_id:red_back_id,
			},
			cache:false,
			timeout : 30000,
			success:function(result) {
				console.log(result);
				if (result.code == 0) {
					var data = result.data;
					if(data){
						window.location.href = data;
					}else{
						alert("充值成功")
						window.location.href = "/personal.html";
					}
				}else{
					alert(result.msg);
					$(".recharge_way").hide();
					window.location.reload();
				}
			},
			error : function() {
				alert('网络异常，请重试');
				window.location.reload();
			}
		});
	}
	checkUserLogin();

	fetchData("/OpenAPI/V1/User/payList",{token:token,client:2,version:1},function(data){
		if(data.weixinList.length > 0){
			var htmlDataList = '';
			for(var i = 0;i < data.weixinList.length;i++){
				htmlDataList += '<div class="recharge-item flex" data-rmb="'+data.weixinList[i]['rmb']+'">';
				htmlDataList += '<img class="icon-money" src="src/img/chongzhizhongxinjinbi.png">';
				htmlDataList += '<div class="diamond"><span>'+data.weixinList[i]['diamond']+'金币</span>';
				htmlDataList += '<span style="color:#a9a9a9;font-size:14px;">赠送'+data.weixinList[i]['present']+'金币</span></div>';
				htmlDataList += '<button type="button" class="align-right inline-block">￥'+data.weixinList[i]['rmb']+'</button>';
				htmlDataList += '</div>';
			}
			$("#htmlDataList").append(htmlDataList);
		}
	})
})
    
		function is_weixin(){
			var ua = navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i)=="micromessenger") {
				return true;
			} else {
				return false;
			}
		}
		if (is_weixin()) {
			$("#wechat").show();
		} else {
			$("#wechatwap").show();
			$("#alipay").show();
        }
	function getCoin()
	{
		var uid = cookieGet("uid");
		var token = cookieGet("token");
		$.ajax( {
			type: "post",
			url:api_url+"/OpenAPI/v1/User/getCoinInfo",
			jsonp: 'callback',
			dataType:'jsonp',
			data:{
				uid: uid,
				token:token
			},
			cache:false,
			timeout : 30000,
			success:function(res) {
					console.log(res);
					var coinbalance = res.data;
					var date = new Date();
					var time = date.getTime();
                    cookieRemove("coinbalance");
					cookieSet("coinbalance",coinbalance,time + 3600*24*30);
					window.location.href="/personal.html";
			},
			error : function() {
				alert('网络异常，请重试');
				window.location.reload();
			}
		});
	}
	function getRedbackList(red_id,rmb){
			$.ajax({
		        type:"post",
		        url:api_url+'/OpenAPI/V1/User/getRedbackList',
		        jsonp:'callback',
				dataType:'jsonp',
		        data:{'token':token},
		        success:function(data){
		            console.log(data)
		            var Li='';
		            var list=data.data;
		            if(red_id){
			            for(var i=0;i<list.length;i++){
							if(red_id==list[i]['back_red_id']){
								myrednum=parseInt(list[i]['coin']);
								rmb=parseInt(rmb);
								console.log(rmb);
								if(myrednum<rmb){
									$("#redbg").html("减"+myrednum+"元")
									$("#moneying").text((rmb-myrednum)+"元");
								}else{
									$("#redbg").html("减"+myrednum+"元")
									$("#moneying").text(0+"元");
								}
							}
						}
					}else{
						$("#moneying").text(rmb+"元");
					}
			    }
		    })
		}
</script>
</html>
